from fastapi import APIRouter, Request, status
from models.db import get_mongo_db
from utils.response import Response
from datetime import datetime

router = APIRouter(prefix="/api", tags=["用户"])

@router.get("/user/info")
async def get_user_info(request: Request):
    """获取当前登录用户信息"""
    # 从request.state中获取用户信息
    if not hasattr(request.state, "user"):
        return Response.error(
            code=status.HTTP_401_UNAUTHORIZED,
            message="未授权访问"
        )
    
    user = request.state.user
    # 返回用户信息
    return Response.success(
        data={
            "id": str(user.get("_id")),
            "username": user.get("username"),
            "email": user.get("email"),
            "gender": user.get("gender", "other"),
            "created_at": user.get("created_at", datetime.now()).isoformat()
        }
    )